package com.xudow.app.newui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.activeshare.edu.ucenter.models.qanda.Question;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiFragment;
import com.xudow.app.ui.P2PQuestionActivity;
import com.xudow.app.ui.QuestionDetailActivity;
import com.xudow.app.ui.adapter.user.QuestionsAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.user.QuestionsTask;
import com.xudow.app.util.Lists;
import com.xudow.app.util.Maps;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 我的提问fragment
 */
public class QuestionsFragment extends BaseUiFragment implements QuestionsAdapter.OnRecyclerViewListener {
    @BindView(R.id.empty_view)
    RelativeLayout emptyView;

    @BindView(R.id.question_list)
    RecyclerView recyclerView;

    @BindView(R.id.progressbar)
    ProgressBar progressBar;

//    @BindView(R.id.sv_view)
//    SwipeRefreshLayout svView;



    private List<Question> questions;
    private QuestionsAdapter questionsAdapter;


    private int page = 1;

    private QuestionsTask questionsTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_questions, container, false);
    }

    @Override
    protected void initData() {
        recyclerView.setHasFixedSize(true);
        XLinearLayoutManager layoutManager = new XLinearLayoutManager(getMyAct());
        layoutManager.setOnRecyclerViewScrollLocationListener(recyclerView,
                new RecyclerViewScrollLocationListener() {
                    @Override
                    public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                    }

                    @Override
                    public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                        //设置页数
                        page++;
                        loadQuestions();
                    }
                });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        questions = Lists.newArrayList();
        questionsAdapter = new QuestionsAdapter(getMyAct(), questions);

        recyclerView.setAdapter(questionsAdapter);


//        svView.setColorSchemeResources(
//                android.R.color.holo_blue_light,
//                android.R.color.holo_green_light,
//                android.R.color.holo_green_light,
//                android.R.color.holo_green_light
//        );
//        svView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                questions=null;
//                page=1;
//                loadQuestions();
//
//                svView.setRefreshing(false);
//
//
//            }
//        });
        loadQuestions();
    }

    @Override
    protected void initListener() {
        questionsAdapter.setOnRecyclerViewListener(this);
    }

    @Override
    public void onItemClick(int position) {
        Question question = questions.get(position);

        //1: 1对1问专家 2：不针对专家问题，任何人都可以回答
        int type = question.getType();

        Intent intent = null;

        if (type == 1) {
            intent = new Intent(getMyAct(), P2PQuestionActivity.class);
        } else {
            intent = new Intent(getMyAct(), QuestionDetailActivity.class);
        }

        intent.putExtra("questionId", question.getId());
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }

    /**
     * 加载我的提问列表数据
     */
    private void loadQuestions() {
        //分页
        HashMap<String, String> params = Maps.newHashMap();
        params.put(Config.PAGINATION_PARAM_NAME, "" + this.page);

        questionsTask = new QuestionsTask(getMyAct(), questionsTaskHandler);
        questionsTask.execute(params);
    }

    private Handler questionsTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (QuestionsTask.SUCCESS == msg.what) {
                Question[] dataArr = (Question[]) msg.getData().getSerializable("questions");

                if (null == dataArr || dataArr.length <= 0) {
                    if (page <= 1) {
                        initEmptyView();
                    }

                    if (page > 1) {
                        page--;
                    }

                    return;
                }

//                progressBar.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

                questions.addAll(Arrays.asList(dataArr));
                questionsAdapter.notifyDataSetChanged();
            }
        }
    };

    private void initEmptyView() {
        progressBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        if (null != questionsTask && !questionsTask.isCancelled()) {
            questionsTask.cancel(true);
            questionsTask = null;
        }
        super.onDestroyView();
    }


}
